package top.mataiji.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mataiji.manager.mapper.BookMapper;
import top.mataiji.manager.mapper.BookimgMapper;
import top.mataiji.manager.mapper.CollectMapper;
import top.mataiji.manager.pojo.Book;
import top.mataiji.manager.pojo.Bookimg;
import top.mataiji.manager.pojo.BookimgExample;
import top.mataiji.manager.pojo.Collect;
import top.mataiji.manager.pojo.vo.CollectVo;
import top.mataiji.manager.service.CollectServer;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zhaoss
 * @Date: 2021/4/9 - 04 - 09 - 15:50
 * @Description: top.mataiji.manager.service.impl
 * @version: 1.0
 */
@Service
public class CollectServerImpl implements CollectServer {

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    BookimgMapper bookimgMapper;

    /**
     * 插入收藏数据
     * @param collect
     * @return
     */
    @Override
    public Integer insertCollect(Collect collect) {
        return collectMapper.insertCollect(collect);
    }

    @Override
    public List<CollectVo> selectCollect(Integer userid) {
        List<CollectVo> collectVos = new ArrayList<>();
        CollectVo collectVo = null;
        //根据userid查询收藏表
        List<Collect> collects = collectMapper.selectAllCollectByUserId(userid);
        System.out.println(collects);
        for (Collect c :  collects) {
            collectVo = new CollectVo();
            //查询书籍
            Integer bookid = c.getBookid();
            Book book = bookMapper.selectByPrimaryKey(bookid);
            collectVo.setBook(book);
            //查询书籍图片
            BookimgExample example = new BookimgExample();
            example.createCriteria().andBookidEqualTo(bookid);
            List<Bookimg> bookimgs = bookimgMapper.selectByExample(example);
            if(!bookimgs.isEmpty()&&bookimgs!=null){
                collectVo.setBookimg(bookimgs.get(0).getBookimageaddress());
            }
            collectVo.setCollect(c);
            collectVos.add(collectVo);
        }
        return collectVos;
    }

    @Override
    public Integer delectCollectById(Integer id) {
        return collectMapper.delectCollectById(id);
    }




}
